package com.wawei.seeds.services;

import android.content.Context;
import android.os.Handler;

import com.wawei.seeds.model.AppModel;
import com.wawei.seeds.services.remote.AppRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutAppRestfulService;
import com.wawei.seeds.utils.RetrofitNetworkService;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by itachi on 15/8/5.
 */
public class WalnutCheckUpdateServiceHandler {

    private Context context;

    public WalnutCheckUpdateServiceHandler(Context context) {
        this.context = context;
    }

    public void checkVersion(RestAdapter restAdapter, final Handler handler, final Integer handlerTag) {
        WalnutAppRestfulService service = restAdapter.create(WalnutAppRestfulService.class);
        service.checkVersion(new Callback<AppRestfulWrapper>() {
            @Override
            public void success(AppRestfulWrapper appRestfulWrapper, Response response) {
                if (appRestfulWrapper != null && appRestfulWrapper.getResultCode() == 200) {
                    AppModel model = new AppModel();
                    model.setQrCode(appRestfulWrapper.getQrCode());
                    model.setUrl(appRestfulWrapper.getUrl());
                    model.setVersion(appRestfulWrapper.getVersion());
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, model);
                } else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }
}
